﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using OccurrencesWeb;
using OccurrencesCore.Infrastructure;

namespace OccurrencesWeb
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            if (!IsOkToDealWithEntityFrameworkContext())
                return;

            UnitOfWork.CommitEntityFrameworkContext();
            UnitOfWork.DisposeEntityFrameworkContext();
        }

        private bool IsOkToDealWithEntityFrameworkContext()
        {
            if (Request.CurrentExecutionFilePathExtension == ".aspx")
                return true;

            return false;
        }

        void RegisterRoutes(RouteCollection routes)
        {
            #region CONFIGURATION
            // Register a route for Configuration list
            routes.MapPageRoute(
               "Configuration List",      
               "Configuration",
               "~/Pages/Configuration/ApplicationConfigurationList.aspx"
            );
            // Register a route for edit a configuration
            routes.MapPageRoute(
                 "Configuration Maintenance",
                 "Configuration/maintenance/{*id}",
                 "~/Pages/Configuration/ApplicationConfigurationEdit.aspx"
             );
            #endregion

            #region OCCURRNCE
            // Register a route for the first page to select the type
            routes.MapPageRoute(
                 "Create new occurrence",
                 "Occurrence/Add",
                 "~/Pages/Occurrence/StartCreation.aspx"
             );
            // Register a route for the creation form
            routes.MapPageRoute(
                 "Create new occurrence - Page 2",
                 "Occurrence/Add/{typeId}",
                 "~/Pages/Occurrence/AddForm.aspx"
             );
            #endregion

            #region [WORKFLOW TASK PARAMETER]
            // Register a route for Configuration list
            routes.MapPageRoute(
               "Workflow Task Parameter List",
               "WorkflowTaskParameter",
               "~/Pages/WorkflowTaskParameter/WorkflowTaskParameterList.aspx"
            );
            // Register a route for edit a configuration
            routes.MapPageRoute(
                 "Workflow Task Parameter Maintenance",
                 "WorkflowTaskParameter/maintenance/{*id}",
                 "~/Pages/WorkflowTaskParameter/WorkflowTaskParameterEdit.aspx"
             );
            #endregion

        }
    }
}
